/*
 * Copyright (c) 2008 VMware, Inc.
 * Copyright (c) 2009 John Pritchard, WTKX Project Group
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package wtkx;

import wtkx.collections.ArrayList;
import wtkx.collections.Dictionary;
import wtkx.collections.Sequence;
import wtkx.io.JSONSerializer;
import wtkx.util.ImmutableIterator;

import java.util.Iterator;

/**
 * Class representing a menu section. A section is a grouping of menu
 * items within a menu.
 *
 * @author gbrown
 */
public class FormSection
    extends Object
    implements Sequence<Component>, Iterable<Component> 
{
    private Form form = null;
    private String heading = null;
    private ArrayList<Component> fields = new ArrayList<Component>();


    public FormSection(){
        super();
    }

    public Form getForm() {
        return form;
    }

    public void setForm(Form form) {
        this.form = form;
    }

    public String getHeading() {
        return heading;
    }

    public void setHeading(String heading) {
        String previousHeading = this.heading;
        if (previousHeading != heading) {
            this.heading = heading;

            if (form != null) {
                form.getFormListeners().sectionHeadingChanged(this);
            }
        }
    }

    public int add(Component field) {
        int index = getLength();
        insert(field, index);

        return index;
    }

    public void insert(Component field, int index) {
        fields.insert(field, index);
        
        if (form != null) {
            form.add(field);
            form.getFormListeners().fieldInserted(this, index);
        }

        field.setAttributes(new Form.FormAttributes(this));
    }

    public Component update(int index, Component field) {
        throw new UnsupportedOperationException();
    }

    public int remove(Component field) {
        int index = fields.indexOf(field);
        if (index != -1) {
            remove(index, 1);
        }

        return index;
    }

    public Sequence<Component> remove(int index, int count) {
        Sequence<Component> removed = fields.remove(index, count);

        for (int i = 0, n = removed.getLength(); i < n; i++) {
            Component field = removed.get(i);
            field.setAttributes(null);

            if (form != null) {
                form.remove(field);
            }
        }

        if (form != null) {
            form.getFormListeners().fieldsRemoved(this, index, removed);
        }

        return removed;
    }

    public Component get(int index) {
        return fields.get(index);
    }

    public int indexOf(Component field) {
        return fields.indexOf(field);
    }

    public int getLength() {
        return fields.getLength();
    }

    public Iterator<Component> iterator() {
        return new ImmutableIterator<Component>(fields.iterator());
    }
}
